package com.yswl.pri.home.vedio.utils;

import com.yswl.pri.Constant;

import java.net.URLDecoder;

import yswl.com.klibrary.util.L;

/**
 * Created by kangpAdministrator on 2018/4/23 0023.
 * Emial kangpeng@yunhetong.net
 */

public class AnalysUtil {
    private final static String iqiyi = "http://www.iqiyi.com/v_";
    private final static String iqiyis = "https://www.iqiyi.com/v_";
    private final static String youkus = "https://v.youku.com/v_show/id_";
    private final static String youku = "http://v.youku.com/v_show/id_";
    private final static String mg = "https://www.mgtv.com/b/";
    private final static String qq = "https://v.qq.com/x/cover/";
    private final static String qq_tv = "https://v.qq.com/tv";


    /**
     * 付费解析
     * 从站点解析 原始播放地址
     *
     * @param url
     * @return
     */
    public static String analysUrlisOk(String url, int type) {
        if (url.endsWith(".js") || url.endsWith(".css")) return "";

        if (type == 0) {
            //优酷 https://v.youku.com/v_nextstage/id_805635e4bb764bd4bf7b.html
            // ?spm=a2hcb.12675304.m_2556_c_8261.d_1&s=805635e4bb764bd4bf7b&scm=20140719.manual.2556.show_805635e4bb764bd4bf7b
            if (url.startsWith(youkus) && url.contains(".html")) {
                return url;
            }
            if (url.startsWith(youku) && url.contains(".html?")) {
                return url;
            }
        }
        if (type == 1) {
            //爱奇艺
            if (url.startsWith(iqiyi) && url.contains(".html") || url.startsWith(iqiyis)&&url.contains(".html")) {
                return url;
            }
        }

        if (type == 2) {
            //腾讯视频
            if (url.startsWith("https://") && url.contains("qq.com/x")) {
                if (url.startsWith(qq) && url.contains(".html")) {
                    return url;
                }
                if (url.contains("&loc=") && url.contains(".html")) {
                    String curr = url.split("&loc=")[1];
                    if (curr.contains(".html"))
                        return curr;
                }
                if ((url.contains("&url=http")||url.contains("&url=https")) && url.contains(".html")) {
                    String curr = url.split("&url=")[1];
                    if (curr.contains(".html"))
                        if (curr.contains("%2F") || curr.contains("%3F"))//说明需要解码
                            curr = URLDecoder.decode(curr);
                    return curr;
                }
                //https://btrace.video.qq.com/kvcollect?BossId=3098&Pwd=390647416&oper=1&main_type=&uin=0&host=v.qq.com&url=https%3A%2F%2Fv.qq.com%2Fx%2Fcover%2F1wbx6hb4d3icse8.html%3Fvid%3Dq0027gkxrfg&_dc=0.694058251510435
                //https://btrace.video.qq.com/kvcollect?BossId=3098&Pwd=390647416&oper=1&main_type=&uin=0&host=v.qq.com&url=https://v.qq.com/x/cover/1wbx6hb4d3icse8.html?vid=q0027gkxrfg&_dc=0.694058251510435
//                if(){
//
//                }

            }
        }
        if (type == 3) {
            //芒果
            //腾讯视频
            if (url.startsWith("https://") && url.contains("mgtv.com")) {
                if (url.startsWith(mg) && url.contains(".html")) {
                    return url;
                }
                if (url.contains("?url=http") && url.contains(".html")) {
                    int currindex = url.indexOf("url=");
                    url = url.substring(currindex + 4, url.length());
                    int currindex2 = url.indexOf(".html");
                    url = url.substring(0, currindex2);
                    if (url.contains("%2F") || url.contains("%3F")) {
                        url = URLDecoder.decode(url);
                        url = url + ".html";
                    }
                    return url;
                }
            }

        }

        return "";
    }

    /**
     * 是否是广告地址
     *
     * @param oldUrl
     * @return
     */
    public static boolean GG(String oldUrl, int type) {
        if (type == 1) {//三大视频网站广告拦截
            if (oldUrl.startsWith("http://img2.xjoot.com") || oldUrl.endsWith(".gif") || oldUrl.startsWith("http://kong") || oldUrl.startsWith("http://x3e")) {
                return true;
            }
            if (oldUrl.contains("snzze.com") || oldUrl.contains("jr.mmuza.com") || oldUrl.contains("po.doublec66.com")) {
                return true;
            }
            if (oldUrl.contains("yun.zuixhd.com") || oldUrl.contains("fp.tongdun.net") || oldUrl.contains("ryylc.msfaxe.com")) {
                return true;
            }
            if (oldUrl.contains("toget.880yo.com") || oldUrl.contains("qlogo.cn") || oldUrl.contains("https://mobile.dk")) {
                return true;
            }
            if (oldUrl.contains("www.jiubuhua")
                    || oldUrl.contains("myhaohaizi.com")
                    || oldUrl.contains("yuyue")
                    || oldUrl.contains("0371xianhua.com")) {
                return true;
            }
        }

        if (type == 2) {//美剧网广告规则
            //去gif广告
            if (oldUrl.contains(".gif") || oldUrl.endsWith(".GIF") || oldUrl.endsWith(".JPG")) {
                return true;
            }

            //过滤广告网站
            if (oldUrl.contains("t.cn")
                    || oldUrl.startsWith("https://pos.baidu.com")
                    || oldUrl.startsWith("https://p.legou361.com")
                    || oldUrl.startsWith("https://hm.baidu.com")
                    || oldUrl.startsWith("http://jy.qc39y.com")
                    || oldUrl.contains("yunpifu.cn")
                    || oldUrl.contains("legou")
                    || oldUrl.contains(".t98u8f.com/")
                    || oldUrl.contains("https://mobile.dk")
                    )
                return true;
            if (oldUrl.contains("app.jxswqp") || oldUrl.contains("dup.baidustatic.com") || oldUrl.contains(".jd.com"))
                return true;
            if (oldUrl.contains("app.ymxqi.com")
                    || oldUrl.contains("app.licai16")
                    || oldUrl.contains("http://www.592meiju.com/img/app.jpg")
                    || oldUrl.contains("ak.1f3k.com"))
                return true;

        }
        if (type == 2) {//韩剧
            //过滤广告网站
            if (oldUrl.startsWith("https://p.legou")
                    || oldUrl.endsWith(".gif")
                    || oldUrl.startsWith("https://m.edo5.com")) {
                return true;
            }
        }

        return false;
    }

    public static String[] host = new String[Constant.URLS.length];
    static {
        for (int i = 0; i < host.length; i++) {
            String url = Constant.URLS[i];
            if (url.startsWith("http")) {
                String curr = url.split("://")[1];
                int index = curr.indexOf("/");
                host[i] = curr.substring(0, index);
            }
        }
    }
    //判断出解析网站地址
    public static boolean G(String oldUrl) {
        for (String uri : host) {
            boolean b = oldUrl.contains(uri);
            if (b) {
                L.e("TAG", "解析器网址 ：" + uri);
                return true;
            }
        }
        return false;
    }
}
